package com.tech.koufu.tools;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.tech.koufu.R;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Environment;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.widget.TextView;

public class KouFuTools {

	public static final SimpleDateFormat sdf_dot = new SimpleDateFormat("yyyy.MM.dd HH:mm");
	public static DecimalFormat df2 = new DecimalFormat("###0.00");
	public static DecimalFormat df3 = new DecimalFormat("###0.000");
	public static final String sdf_date = "yyyy-MM-dd";

	public static String readTimeInfo(String t){
		long time=System.currentTimeMillis();
		SimpleDateFormat format=new SimpleDateFormat("HHmmss");  
		Date d=new Date(time);  
		t=format.format(d); 
		Log.v("Tools_Time", t);
		return t;
	}

	/** 日期转时间戳 日期,格式"yyyy-MM-dd HH:mm:ss" 转换失败返回-1 */
	public static long date2unix(String date) {
		try {
			return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date)
					.getTime()
					- new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(
							"1970-01-01 00:00:00").getTime();
		} catch (ParseException e) {
			return -1;
		}
	}

	public static String descrypt(String input){
		CoFoolDes crypt = new CoFoolDes();
		String value = crypt.DES_EnCrypt(input, Statics.DECODE_KEY);
		Log.v("DEScrypt====>", value);
		return "0"+value;		
	}
	/**
	 * 文件缓存路径
	 * @param context
	 * @param uniqueName
	 * @return
	 */
	public static  File getDiskCacheDir(Context context, String uniqueName) {
		String cachePath;
		if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
				|| !Environment.isExternalStorageRemovable()) {
			cachePath = context.getExternalCacheDir().getPath();
		} else {
			cachePath = context.getCacheDir().getPath();
		}
		return new File(cachePath + File.separator + uniqueName);
	}
	/**
	 * 
	 * @Title: getAppVersion 
	 * @Description: TODO(版本code) 
	 * @param @param context
	 * @param @return    设定文件 
	 * @return int    返回类型 
	 * @throws
	 */
	public static int getAppVersion(Context context) {
		try {
			PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
			return info.versionCode;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return 1;
	}

	/**
	 * 
	 * @Title: hashKeyForDisk 
	 * @Description: TODO( ) 
	 * @param @param key
	 * @param @return    设定文件 
	 * @return String    返回类型 
	 * @throws
	 */
	public static String hashKeyForDisk(String key) {
		String cacheKey;
		try {
			final MessageDigest mDigest = MessageDigest.getInstance("MD5");
			mDigest.update(key.getBytes());
			cacheKey = bytesToHexString(mDigest.digest());
		} catch (NoSuchAlgorithmException e) {
			cacheKey = String.valueOf(key.hashCode());
		}
		return cacheKey;
	}

	private static String bytesToHexString(byte[] bytes) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < bytes.length; i++) {
			String hex = Integer.toHexString(0xFF & bytes[i]);
			if (hex.length() == 1) {
				sb.append('0');
			}
			sb.append(hex);
		}
		return sb.toString();
	}

	public static String inputStream2String (InputStream in) throws IOException   {   

		StringBuffer out = new StringBuffer();   
		byte[]  b = new byte[4096];   
		int n;  
		while ((n = in.read(b))!= -1){   
			out.append(new String(b,0,n));   
		}   
		return  out.toString();   
	} 


	public static String getEntrustType(String type){
		if(type.equals("c")){
			return "卖出";
		}else if(type.equals("r")){
			return "买入";
		}else if (type.equals("d")){
			return "撤单"; 
		}
		return "";
	}

	public static String getEnCode(String code) {
		if(code!=null){
			String _pre = code.substring(0, 2);
			if(Integer.parseInt(_pre)>40){
				return "SH";
			}else{
				return "SZ";
			}
		}
		return code;
	}

	public static String getNcode(String code) {
		if(code!=null){
			String _pre = code.substring(0, 2);
			if(Integer.parseInt(_pre)>40){
				return "1"+code;
			}else{
				return "0"+code;
			}
		}
		return code;
	}
	public static String stockGetZQLB(int stockId,String stockName){
		String zqlb = null;
		if(stockId>=1 && stockId<=9999){
			zqlb = "A";
		}else if(stockId>=30000 && stockId<=39999){
			zqlb = "Z";
		}else if (stockId>=101800 && stockId<=119999) {
			zqlb = "Q";
		}else if (stockId>=200000 && stockId<=209999){
			zqlb = "B";
		}else if(stockId>=150000 && stockId<=169999){
			if(stockId>=150001 && stockId<=150003){
				zqlb = "F";
			}else if(stockId>=159900 && stockId<=159999){
				zqlb = "E";
			}else{
				zqlb = "L";
			}
		}else if(stockId>=170000 && stockId<=187999){
			zqlb = "F";
		}else if(stockId>=189000 && stockId<=189999){
			zqlb = "F";
		}else if(stockId>=300000 && stockId<=309999){
			zqlb = "C";
		}else if(stockId>=500000 && stockId<=500599){
			zqlb = "F";
		}else if(stockId>=510000 && stockId<=510999){
			zqlb = "E";
		}else if(stockId>=550000 && stockId<=550999){
			zqlb = "F";
		}else if(stockId>=510000 && stockId<=510999){
			zqlb = "E";
		}else if(stockId>=580000 && stockId<=580999){
			zqlb = "Z";
		}else if(stockId>=600000 && stockId<=601999){
			zqlb = "A";
		}else if(stockId>=609000 && stockId<=609999){
			zqlb = "A";
		}else if(stockId>=900000 && stockId<=999999){
			zqlb = "B";
		}
		if(stockName.contains("N")){
			zqlb = "N";
		}
		return zqlb; 
	}
	public static void setTextColor(int color,TextView view,int start ,int end,String txt){
		SpannableString style=new SpannableString(txt);           
		style.setSpan(new ForegroundColorSpan(color),start,end,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);       
		view.setText(style);  
	}


	public static void setValueColor(Context context,TextView view,int start ,int end,String txt,String formattxt){

		int yll_color = R.color.TextColorRed_FD0000;
		if(txt.startsWith("-")){
			yll_color = R.color.txtColorGreen_21d400;
		} 
		//		txt = String.format(format, txt);
		yll_color = context.getResources().getColor(yll_color);
		SpannableString style=new SpannableString(formattxt);           
		style.setSpan(new ForegroundColorSpan(yll_color),start,end,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);       
		view.setText(style);  
	} 

	public static int getStockValueColor(String txt){
		int ycolor = R.color.TextColorRed_FD0000;
		if(null!=txt && txt.startsWith("-")){
			return  R.color.txtColorGreen_21d400;
		}
		
		return ycolor;
	}
	/**
	 * 手续费计算
	 * @param 
	 * String 可用资金 或者 可卖股数
	 * String 委托价格
	 * String 佣金比例
	 */
	public static String getCommission(String canUserOrCanSale,String wtprice,String flk){
		String Commission = null;

		Commission = ""+(Float.parseFloat(canUserOrCanSale)/(Float.parseFloat(wtprice)*(1+Float.parseFloat(flk)))/100*100);

		return Commission;		
	}
	
	/**
	 * 
	 * 开盘时间计算
	 * @return
	 */
	public static String KaiPanTimeCount(){
		String value = null;
		Calendar calendar = Calendar.getInstance();
		int day = calendar.get(calendar.DAY_OF_YEAR );
		int nowhour = calendar.get(calendar.HOUR_OF_DAY);
		if((day%7 -4) == 0 && ishoilday() ==false){			
			if(nowhour<9){
				value = "距离开盘还有1天" + (8-nowhour) +"小时";
			}else{
				value = "距离开盘还有"+ (24+8-nowhour) + "小时" + (60-calendar.get(calendar.MINUTE))+ "分钟";
			}			
		}else if(day%7 -3 == 0 && ishoilday() ==false){
			if(nowhour<9){
				value = "距离开盘还有2天" + (8-nowhour) +"小时";
			}else{
				value = "距离开盘还有1天"+ (24+8-nowhour) + "小时";
			}
		}else if(ishoilday() == true){
			value = "股市休市中";
		}else{
			if(nowhour<9){
				value = "距离开盘还有"+ (8-nowhour) + "小时" + (60-calendar.get(calendar.MINUTE))+ "分钟";
			}else if(nowhour >= 16){
				value = "距离开盘还有"+ (24+8-nowhour) + "小时" + (60-calendar.get(calendar.MINUTE))+ "分钟";
			}else{
				value = "开盘中";
			}
		}		
		return value;
	}
	
	public static boolean ishoilday(){
		Calendar calendar = Calendar.getInstance();
		int day = calendar.get(calendar.DAY_OF_YEAR );
		if(day >= 171 && day <=173 || day >=246 && day <= 248 || day >=274 && day <= 280){
			return true;
		}else{
			return false;
		}		
	}
	
	public static String UrlChoose(int webId){
		String url = null;
		if(webId == 100){
			url = Statics.URL_1;
		}else if(webId == 101){
			url = Statics.URL_2;
		}else if(webId == 110){
			url = Statics.URL_3;
		}
		return url;		
	}
}
